var _urlCommon=sessionStorage.getItem("_urlCommon");
$.baseUrl = function (method) {
    if (_urlCommon) {
        return _urlCommon;
    }
    _urlCommon = window.location.href;
    var index = 0;
    for (var i = 0; i < 4; i++) {
        index = _urlCommon.indexOf('/', index + 1);
    }
    _urlCommon = _urlCommon.substring(0, index + 1);
    if(method){
        _urlCommon=method+_urlCommon.match(/:.+/)[0]
    }
    sessionStorage.setItem("_urlCommon",_urlCommon);
    return _urlCommon;
};

$.urlSuffix=function () {
    return "";
}
var _k8sUrlCommon=sessionStorage.getItem("common_k8s_url");
$.k8sUrl = function (method) {
    if (_k8sUrlCommon) {
        return _k8sUrlCommon;
    }
    _k8sUrlCommon = window.location.href;
    var index = 0;
    for (var i = 0; i < 4; i++) {
        index = _k8sUrlCommon.indexOf('/', index + 1);
    }
    _k8sUrlCommon = _k8sUrlCommon.substring(0, index + 1);
    if (method) {
        _k8sUrlCommon = method + _k8sUrlCommon.match(/:.+/)[0]
    }
    sessionStorage.setItem("common_k8s_url",_k8sUrlCommon);
    return _k8sUrlCommon;
};
$.getChromeVersion = function (isFullVersion) {
    var ua = navigator.userAgent.toUpperCase();
    var reg = /CHROME\/([\d]+)/;
    if (isFullVersion) {
        reg = /CHROME\/([\d.]+)/;
    }
    var vserion = ua.match(reg);
    return vserion && vserion[1] ? vserion[1] : '';
};
$.extend({
    'getQueryStr': function(str) {
        var reg = new RegExp("(^|&)" + str + "=([^&]*)(&|$)");
        var r = decodeURIComponent(window.location.search).substr(1).match(reg);
        return r != null ? unescape(r[2]) : null;
    }
});
jQuery.prototype.serializeObject=function(){
    var obj=new Object();
    $.each(this.serializeArray(),function(index,param){
        if(!(param.name in obj)){
            obj[param.name]=param.value;
        }
    });
    return obj;
};
var zwpc_skin = localStorage.getItem("zwpc_skin"),_k8s_url=sessionStorage.getItem("common_k8s_url");
var skin_style;
if(zwpc_skin){
    skin_style = zwpc_skin;
    $('link[href*="resourcesV3/other/favicon.ico"]').attr('href','img/img_'+skin_style+'/resourcesV3/other/favicon.ico');
}else{
    localStorage.setItem("zwpc_skin","default");
    skin_style = 'default';
}
//备份jquery的ajax方法
var _ajax=$.ajax;

//重写jquery的ajax方法
$.ajax=function(opt){
    //备份opt中error和success方法
    var fn = {
        error:function(XMLHttpRequest, textStatus, errorThrown){},
        success:function(data, textStatus){}
    }
    if(opt.success){
        fn.success=opt.success;
    }
    if(opt.error){
        fn.error=opt.error;
    }
    if(opt.data){
        opt.data.requestTime = new Date().getTime();
    }else{
        opt.data={requestTime:new Date().getTime()}
    }
    if(opt.url)opt.url=opt.url+$.urlSuffix();
    var authorization=localStorage.getItem("token");
    //扩展增强处理
    var _opt = $.extend(opt,{
        beforeSend: function (xhr) {
            if(authorization){
                xhr.setRequestHeader("Authorization",authorization);
            }
        },success:function(data, textStatus,xhr){
            var token = xhr.getResponseHeader("x-auth-token")
            if (token) {
                localStorage.setItem("token",token);
            }
            var noLogin = xhr.getResponseHeader("noLogin")
            if(noLogin){
                layer.msg(data.msg);
                setTimeout(function () {
                    localStorage.setItem("token","");
                    window.location.replace($.baseUrl()+"login.html");
                },300);
            }
            //成功回调方法增强处理
            fn.success(data, textStatus);
        },
        error:function (xhr, textStatus, errorThrown) {
            if (typeof error === "function") {
                opt.error(this, xhr, textStatus, errorThrown);
            }
        }
    });
    _ajax(_opt);
};
//获取html页面参数
function getQueryVar(variable) {
    var query=window.location.search.substring(1);
    var vars=query.split("&");
    for(var i=0;i<vars.length;i++){
        var pair=vars[i].split("=");
        if(pair[0]==variable){
            return pair[1];
        }
    }
    return "";
}
Date.prototype.Format = function(fmt)
{
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)){
        fmt = fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o){
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
};

function getWeekFirstDay(date){
    var day = date.getDay();
    var minus = 1-day;
    date.setDate(date.getDate()+minus);
    return date;
}

//获取当年第一天
 function getFirstDayOfYear(date) {
     date.setDate(1);
     date.setMonth(0);
     return date.format("yyyy-MM-dd");
 }

 function getLastDayOfYear(date){
     var  year = date.getFullYear();
     var lastdate = year + '-' + 12+ '-' + 31;
     return lastdate;
 }

function getWeekLastDay(date){
    var day = date.getDay();
    var minus = 7-day;
    date.setDate(date.getDate()+minus);
    return date;
}

function getOrgType(orgId){
    var start = orgId.length-4;
    var arr = orgId.substr(start,4);
    if(orgId=='330000001'){
        return 'sgs';
    }else if(arr=='0000'){
        return 'sj';
    }else{
        return 'xj';
    }

}

function initSelect(data,val){
    for(var i=0;i<data.length;i++){
        var ss = data[i];
        if(val==ss[0]){
            return i+1;
        }
        if(val==ss[1]){
            return i+1;
        }
    }
}

function initMultiSelect(data,val){
    if(typeof(val) == 'undefined') {
        return;
    }
    var result = "";
    for(var i=0;i<data.length;i++){
        var ss = data[i];
        if(val.indexOf(ss[0])!=-1){
            result+=(i+1)+",";
        }else if(val.indexOf(ss[1])!=-1){
            result+=(i+1)+",";
        }
    }
    if(result!=''){
        result = "'"+result.substr(0,result.length-1)+"'";
    }
    console.log(result)
    return result;
}